package org.example.im.server;

import lombok.RequiredArgsConstructor;
import org.example.im.server.netty.NettyServerBootstrap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.net.InetSocketAddress;

/**
 * @author dingrui
 * @since 2022/2/10
 * @description
 */
@SpringBootApplication
@ComponentScan("org.example.im")
@RequiredArgsConstructor
public class IMServerApplication implements CommandLineRunner {

    @Value("${netty.port:12000}")
    private Integer port;

    private final NettyServerBootstrap nettyServerBootstrap;

    public static void main(String[] args) {
        SpringApplication.run(IMServerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(this.port);
        this.nettyServerBootstrap.start(address);
        // 虚拟机钩子
        Runtime.getRuntime().addShutdownHook(new Thread(this.nettyServerBootstrap::destroy));
    }
}
